package spring.factoryBean;

import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.config.FieldRetrievingFactoryBean;
import org.springframework.beans.factory.config.MethodInvokingFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

/**
 * FactoryBean 测试：枚举声明配置类，直接 @Bean 返回枚举对象就完了
 *
 * @author ME
 * @date   2020/5/10
 */
@Configuration
@ComponentScan("spring.factoryBean")
public class FactoryBeanConfig {

    @Bean
    // @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE) // 真实对象会继承`FactoryBean`的 @Scope配置
    public StudentFactoryBean studentFactoryBean() {
        return new StudentFactoryBean();
    }

    @Bean
    public ColorEnum red(ApplicationContext context) {
        ColorEnum.RED.setContext(context);
        return ColorEnum.RED;
    }

    /**
     * Field 的值声明为 Bean：  <p>
     * 1，setStaticField 设置静态属性：class.filedName  <p>
     * 2，setTargetClass、setTargetField 设置静态属性  <p>
     * 3，setTargetObject、setTargetField 设置成员变量  <p>
     *
     * @return
     */
    @Bean
    public FieldRetrievingFactoryBean blank() {
        FieldRetrievingFactoryBean factoryBean = new FieldRetrievingFactoryBean();
        // factoryBean.setStaticField("spring.factoryBean.ColorEnum.BLANK");
        factoryBean.setTargetClass(ColorEnum.class);
        factoryBean.setTargetField("BLANK");
        return factoryBean;
    }

    /**
     * Method 的返回值声明为 Bean：  <p>
     * 1，setStaticMethod 设置静态方法：class.methodName  <p>
     * 2，setTargetClass、setTargetMethod 设置静态方法  <p>
     * 3，setTargetObject、setTargetMethod 设置成员方法  <p>
     *
     * @return
     */
    // @Bean
    public MethodInvokingFactoryBean methodInvokingFactoryBean() {
        MethodInvokingFactoryBean factoryBean = new MethodInvokingFactoryBean();
        factoryBean.setStaticMethod("spring.factoryBean.ColorEnum.BLANK");
        // factoryBean.setTargetClass(XX.class);
        // factoryBean.setTargetMethod("methodName");
        return factoryBean;
    }

    @Bean
    public ColorEnum yellow(ApplicationContext context) {
        ColorEnum.YELLOW.setContext(context);
        return ColorEnum.YELLOW;
    }

}
